package nlp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.tartarus.snowball.ext.porterStemmer;

import weka.core.tokenizers.NGramTokenizer;

public class NGramFeatureCreator implements FeatureCreator {

	private String textString;
	private porterStemmer stemmer;

	public NGramFeatureCreator(String textString, porterStemmer stemmer) {
		this.textString = textString;
		this.stemmer = stemmer;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void CreateFeature(Object obj) {
		// Tokenize the text.
		int ngramSize = 3;
		NGramTokenizer ngramTokenizer = new NGramTokenizer();
		ngramTokenizer.tokenize(textString);
		ngramTokenizer.setNGramMaxSize(ngramSize);
		ngramTokenizer.setNGramMinSize(ngramSize);

		List<String> ngrams = new ArrayList<String>();
		while (ngramTokenizer.hasMoreElements()) {
			String ngram = (String) ngramTokenizer.nextElement();
			if (AuthorNameFilter.ContainsAuthorName(ngram)) {
				continue;
			}
			this.stemmer.setCurrent(ngram.toLowerCase());
			this.stemmer.stem();
			String stemmedNgram = stemmer.getCurrent();
			ngrams.add(stemmedNgram);
		}

		FreqDistUtil freqDistUtil = new FreqDistUtil(ngrams, ngrams.size());
		List<FreqData> freqDist = freqDistUtil.getTop(100);
		HashMap<String, Feature> featureMap = (HashMap<String, Feature>) obj;
		for (FreqData data : freqDist) {
			Feature feature = new NGramFeature(data.getData(), data
					.getFrequency());
			featureMap.put(feature.GetFeatureName(), feature);
		}
	}

	@Override
	public String GetFeatureName() {
		// TODO Auto-generated method stub
		return "NGramTop100";
	}

}
